﻿using System;
using System.Text;
using System.Web.Mvc;
using MvcForum.Net.Business;
using SubSonic.Repository;

namespace MvcForum.Net.Controllers
{
    public class AdminController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Forum()
        {
            return View();
        }

        public ActionResult Roles()
        {
            return View();
        }



        public ActionResult ForumModel()
        {
            return PartialView();
        }


        public ActionResult ForumList()
        {
            // PagedList<Forum> _forums = Business.Forum.GetPaged(1, 20);
            // return PartialView("ForumList", _forums);
            return View();

        }


        //[ControllerAction]
        // public string ForumList()
        // {
        //     PagedList<Forum> _forums = Business.Forum.GetPaged(1, 20);

        //    return RenderPartialExtensions.RenderPartialToString("ForumList", _forums); 
        //     //return PartialView("ForumList", _forums);
        // }


        [ValidateAntiForgeryToken(Salt = "ROLESAVE"), HttpPost]
        public ActionResult RoleSave(FormCollection form)
        {

            //int _id = int.Parse(form["RoleId"]);
            //var _db = new MainDB();


            #region Accessmask

            var _accessMask = new StringBuilder();

            if ((form["AllowPriority"]) != null)
            {
                //Convert.ToBoolean()
                _accessMask.AppendFormat("AllowPriority:{0}", Convert.ToBoolean(form["AllowPriority"]));
                _accessMask.Append(",");
            }


            if ((form["PostNewTopics"]) != null)
            {
                _accessMask.AppendFormat("PostNewTopics:{0}", Convert.ToBoolean(form["PostNewTopics"]));
                _accessMask.Append(",");
            }

            if ((form["PostReplyToTopics"]) != null)
            {
                _accessMask.AppendFormat("PostReplyToTopics:{0}", Convert.ToBoolean(form["PostReplyToTopics"]));
                _accessMask.Append(",");
            }


            if ((form["PostNewPolls"]) != null)
            {
                _accessMask.AppendFormat("PostNewPolls:{0}", Convert.ToBoolean(form["PostNewPolls"]));
                _accessMask.Append(",");
            }

            if ((form["MaxPollAnswers"]) != null)
            {
                _accessMask.AppendFormat("MaxPollAnswers:{0}", form["MaxPollAnswers"]);
                _accessMask.Append(",");
            }


            if ((form["PostReplyPolls"]) != null)
            {
                _accessMask.AppendFormat("PostReplyPolls:{0}", Convert.ToBoolean(form["PostReplyPolls"]));
                _accessMask.Append(",");
            }


            if ((form["VoteWithinPolls"]) != null)
            {
                _accessMask.AppendFormat("VoteWithinPolls:{0}", Convert.ToBoolean(form["VoteWithinPolls"]));
                _accessMask.Append(",");
            }


            if ((form["PostWithinLockedForums"]) != null)
            {
                _accessMask.AppendFormat("PostWithinLockedForums:{0}", Convert.ToBoolean(form["PostWithinLockedForums"]));
                _accessMask.Append(",");
            }

            if ((form["PostWithinLockedTopics"]) != null)
            {
                _accessMask.AppendFormat("PostWithinLockedTopics:{0}", Convert.ToBoolean(form["PostWithinLockedTopics"]));
                _accessMask.Append(",");
            }


            if ((form["EditOwnTopics"]) != null)
            {
                _accessMask.AppendFormat("EditOwnTopics:{0}", Convert.ToBoolean(form["EditOwnTopics"]));
                _accessMask.Append(",");
            }

            if ((form["EditOwnPosts"]) != null)
            {
                _accessMask.AppendFormat("EditOwnPosts:{0}", Convert.ToBoolean(form["EditOwnPosts"]));
                _accessMask.Append(",");
            }

            if ((form["EditOtherMembersTopics"]) != null)
            {
                _accessMask.AppendFormat("EditOtherMembersTopics:{0}", Convert.ToBoolean(form["EditOtherMembersTopics"]));
                _accessMask.Append(",");
            }


            if ((form["ViewLastEditedByLabel"]) != null)
            {
                _accessMask.AppendFormat("ViewLastEditedByLabel:{0}", Convert.ToBoolean(form["ViewLastEditedByLabel"]));
                _accessMask.Append(",");
            }

            if ((form["UpdateLastEditedBy"]) != null)
            {
                _accessMask.AppendFormat("UpdateLastEditedBy:{0}", Convert.ToBoolean(form["UpdateLastEditedBy"]));
                _accessMask.Append(",");
            }



            if ((form["DeleteOwnTopics"]) != null)
            {
                _accessMask.AppendFormat("DeleteOwnTopics:{0}", Convert.ToBoolean(form["DeleteOwnTopics"]));
                _accessMask.Append(",");
            }


            if ((form["DeleteOwnPosts"]) != null)
            {
                _accessMask.AppendFormat("DeleteOwnPosts:{0}", Convert.ToBoolean(form["DeleteOwnPosts"]));
                _accessMask.Append(",");
            }

            if ((form["DeleteOtherMembersTopics"]) != null)
            {
                _accessMask.AppendFormat("DeleteOtherMembersTopics:{0}", Convert.ToBoolean(form["DeleteOtherMembersTopics"]));
                _accessMask.Append(",");
            }


            if ((form["DeleteOtherMembersPosts"]) != null)
            {
                _accessMask.AppendFormat("DeleteOtherMembersPosts:{0}", Convert.ToBoolean(form["DeleteOtherMembersPosts"]));
                _accessMask.Append(",");
            }


            if ((form["AlwaysPostAprroved"]) != null)
            {
                _accessMask.AppendFormat("AlwaysPostAprroved:{0}", Convert.ToBoolean(form["AlwaysPostAprroved"]));
                _accessMask.Append(",");
            }


            if ((form["QueueEdited"]) != null)
            {
                _accessMask.AppendFormat("QueueEdited:{0}", Convert.ToBoolean(form["QueueEdited"]));
                _accessMask.Append(",");
            }

            if ((form["UploadSize"]) != null)
            {
                _accessMask.AppendFormat("UploadSize:{0}", int.Parse(form["UploadSize"]));
                _accessMask.Append(",");
            }

            if ((form["AllowDownloadAttachments"]) != null)
            {
                _accessMask.AppendFormat("AllowDownloadAttachments:{0}", Convert.ToBoolean(form["AllowDownloadAttachments"]));
                _accessMask.Append(",");
            }


            if ((form["ViewSearch"]) != null)
            {
                _accessMask.AppendFormat("ViewSearch:{0}", Convert.ToBoolean(form["ViewSearch"]));
                _accessMask.Append(",");
            }


            if ((form["MaxSearchResults"]) != null)
            {
                _accessMask.AppendFormat("MaxSearchResults:{0}", int.Parse(form["MaxSearchResults"]));
                _accessMask.Append(",");
            }

            if ((form["MaxSearchKeywordLength"]) != null)
            {
                _accessMask.AppendFormat("MaxSearchKeywordLength:{0}", int.Parse(form["MaxSearchKeywordLength"]));
                _accessMask.Append(",");
            }


            _accessMask.AppendFormat("SearchResultsPerPage:{0}", (form["SearchResultsPerPage"]));
            _accessMask.Append(",");


            if ((form["SendEmails"]) != null)
            {
                _accessMask.AppendFormat("SendEmails:{0}", Convert.ToBoolean(form["SendEmails"]));
                _accessMask.Append(",");
            }


            if ((form["MaxEmailRecipients"]) != null)
            {
                _accessMask.AppendFormat("MaxEmailRecipients:{0}", int.Parse(form["MaxEmailRecipients"]));
                _accessMask.Append(",");
            }


            #endregion

            //_db.RoleSave(_id,
            //        form["RoleName"],
            //      bool.Parse(form["IsAutoStart"]),
            //      bool.Parse(form["IsAdmin"]),
            //      form["CssClass"],
            //      _accessMask.ToString()


            //       ).Execute();

            return Json(new
            {

                sussess = true,
                message = _accessMask.ToString()
            });

        }


        [ValidateAntiForgeryToken(Salt = "FORUMSAVE"), HttpPost]
        [ValidateInput(false)]
        public ActionResult ForumSave(FormCollection form)
        {

            //Forum _object;
            int _id = int.Parse(form["ForumId"]);
            var _db = new MainDB();

            _db.ForumSave(_id,
                   form["ForumName"],
                   int.Parse(form["ParentId"]),
                   form["Keywords"],
                   form["Description"],
                   short.Parse(form["SortOrder"]),
                   form["RemoteUrl"],
                   bool.Parse(form["IsLocked"]),
                   bool.Parse(form["AllowPostWithIn"]),
                   short.Parse(form["ModerateType"]),
                   form["AllowRoles"],
                   bool.Parse(form["VisibleAtHomePage"])
                   ).Execute();

            return Json(new
                            {

                                sussess = true,
                                message = "Update successfull"
                            });
        }






    }
}

